home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gcctest / tests05.zoo / tstdio.c < prev    next >
C/C++ Source or Header  |  1993-03-02  |  18KB  |  965 lines

  1. /*            E x e r c i s e
  2.  *
  3.  * This program exercises the stdio routines. It does not validate the
  4.  * routines but provides a convenient way to quickly check the functionality
  5.  * of the code.
  6.  */
  7.  
  8. #ifdef _BSD
  9. # include <strings.h>
  10. # define remove unlink
  11. # define sleep(x)    usleep((x)*500)
  12. #else
  13. # include <stdlib.h>
  14. # include <string.h>
  15. #endif
  16. #include <stdio.h>
  17.  
  18. #ifdef atarist
  19. #include <memory.h>
  20. #include <unistd.h>
  21. #endif
  22.  
  23. #ifndef SEEK_SET
  24. /* lseek() origins */
  25. #define    SEEK_SET    0        /* from beginning of file */
  26. #define    SEEK_CUR    1        /* from current location */
  27. #define    SEEK_END    2        /* from end of file */
  28. #endif
  29.  
  30. #define UCHAR(x)    ((int) ((x) & 0xff))
  31.  
  32. #define DEC -123
  33. #define INT 255
  34. #define UNS (~0)
  35. #define TESTFILE    "test.dat"
  36. #define LARGEBUFS    16
  37. #ifdef        MSDOS
  38. # define    TTY    "con"
  39. #else
  40. # ifdef atarist
  41. #  define    TTY    "CON:"
  42. # else
  43. #  define    TTY    "/dev/tty"
  44. # endif
  45. #endif
  46.  
  47. #ifndef atarist
  48. extern void *malloc();            /* memory allocator */
  49. extern char *strcpy();            /* string copy */
  50. extern char *strcat();            /* string concatenation */
  51. extern int strcmp();            /* string compare */
  52. extern void exit();            /* exit */
  53. #endif
  54.  
  55. FILE *fp;                /* per test file pointer */
  56.  
  57. /*
  58.  * Line Buffered Write Test
  59.  *
  60.  * Write to a terminal. This tests that the output buffer is
  61.  * flushed on receipt of a \n.
  62.  */
  63.  
  64. void lbw_test()
  65.  
  66. {
  67.   int i;
  68.  
  69.   puts("\nLine buffered write test");
  70. #ifdef atarist
  71.   if ((fp = fopen(TTY, "wt")) != NULL) {
  72. #else
  73.   if ((fp = fopen(TTY, "w")) != NULL) {
  74. #endif
  75.     puts("<pause>ABCDEFGH");
  76.     puts("<pause>ABCD<pause>EFGH");
  77.     for (i = 0; i < 8; i++)
  78.       putc('A'+i, fp), sleep(1);
  79.     putc('\n', fp);
  80.     for (i = 0; i < 8; i++) {
  81.       putc('A'+i, fp);
  82.       if (i == 3)
  83.     fflush(fp);
  84.       sleep(1);
  85.     }
  86.     fclose(fp);
  87.     puts("");
  88.   }
  89. }
  90.  
  91. /*
  92.  * Unbuffered Write Test
  93.  *
  94.  * Test that characters are written directly to the output device
  95.  * when the stream is unbuffered.
  96.  */
  97.  
  98. void ubw_test()
  99.  
  100. {
  101.   int i;
  102.  
  103.   puts("\nUnbuffered write test");
  104. #ifdef atarist
  105.   if ((fp = fopen(TTY, "wt")) != NULL) {
  106. #else
  107.   if ((fp = fopen(TTY, "w")) != NULL) {
  108. #endif
  109.     setbuf(fp, (char *) 0);
  110.     puts("A<pause>B<pause>C<pause>D<pause>E<pause>F<pause>G<pause>H<pause>");
  111.     puts("A<pause>B<pause>C<pause>D<pause>E<pause>F<pause>G<pause>H<pause>");
  112.     for (i = 0; i < 8; i++)
  113.       putc('A'+i, fp), sleep(1);
  114.     putc('\n', fp);
  115.     for (i = 0; i < 8; i++)
  116.       putc('A'+i, fp), sleep(1);
  117.     fclose(fp);
  118.     puts("");
  119.   }
  120. }
  121.  
  122. /*
  123.  * Buffered Write Test
  124.  *
  125.  * Test that the data is written to the terminal on a per buffer
  126.  * basis.
  127.  */
  128.  
  129. void bw_test()
  130.  
  131. {
  132.   int i;
  133.  
  134.   puts("\nFully buffered write test");
  135. #ifdef atarist
  136.   if ((fp = fopen(TTY, "wt")) != NULL) {
  137. #else
  138.   if ((fp = fopen(TTY, "w")) != NULL) {
  139. #endif
  140.     setvbuf(fp, (char *) 0, _IOFBF, 4);
  141.     puts("<pause>ABCD<pause>EFGH<pause>");
  142. #ifdef atarist
  143.     /* in text mode when we putc('\n', fp) we add a '\r' so we are off
  144.        by one char
  145.      */
  146.     puts("AB<pause>CDEF<pause>GH<pause>");
  147. #else
  148.     puts("ABC<pause>DEFG<pause>H<pause>");
  149. #endif
  150.     for (i = 0; i < 8; i++)
  151.       putc('A'+i, fp), sleep(1);
  152.     putc('\n', fp);
  153.     for (i = 0; i < 8; i++)
  154.       putc('A'+i, fp), sleep(1);
  155.     fclose(fp);
  156.     sleep(2);
  157.     puts("");
  158.   }
  159. }
  160.  
  161. /* Formatted Output Test
  162.  *
  163.  * This exercises the output formatting code.
  164.  */
  165.  
  166. void fp_test()
  167.  
  168. {
  169.   int i, j, k, l;
  170.   char buf[7];
  171.   char *prefix = buf;
  172.   char tp[20];
  173.  
  174.   puts("\nFormatted output test");
  175.   printf("prefix  6d      6o      6x      6X      6u\n");
  176.   strcpy(prefix, "%");
  177.   for (i = 0; i < 2; i++) {
  178.     for (j = 0; j < 2; j++) {
  179.       for (k = 0; k < 2; k++) {
  180.     for (l = 0; l < 2; l++) {
  181.       strcpy(prefix, "%");
  182.       if (i == 0) strcat(prefix, "-");
  183.       if (j == 0) strcat(prefix, "+");
  184.       if (k == 0) strcat(prefix, "#");
  185.       if (l == 0) strcat(prefix, "0");
  186.       printf("%5s |", prefix);
  187.       strcpy(tp, prefix);
  188.       strcat(tp, "6d |");
  189.       printf(tp, DEC);
  190.       strcpy(tp, prefix);
  191.       strcat(tp, "6o |");
  192.       printf(tp, INT);
  193.       strcpy(tp, prefix);
  194.       strcat(tp, "6x |");
  195.       printf(tp, INT);
  196.       strcpy(tp, prefix);
  197.       strcat(tp, "6X |");
  198.       printf(tp, INT);
  199.       strcpy(tp, prefix);
  200.       strcat(tp, "6u |");
  201.       printf(tp, UNS);
  202.       printf("\n");
  203.     }
  204.       }
  205.     }
  206.   }
  207. }
  208.  
  209. /*
  210.  * String Output Test
  211.  *
  212.  * Test the string printf code.
  213.  */
  214.  
  215. void sw_test()
  216.  
  217. {
  218.   int i;
  219.   char buf[80];
  220.  
  221.   puts("\nTest sprintf functionality");
  222.   puts("13 bytes in 'Testing 1 2 3'");
  223.   i = sprintf(buf, "Testing %d %d %d", 1, 2, 3);
  224.   printf("%d bytes in '%s'\n", i, buf);
  225. }
  226.  
  227. /*
  228.  * String Input Test
  229.  *
  230.  * Test the string scanf code.
  231.  */
  232.  
  233. void sr_test()
  234.  
  235. {
  236.   int i, j;
  237.   char buf[80];
  238.  
  239.   puts("\nTest sscanf functionality");
  240.   puts("2 items yielding 25 and 'thompson'");
  241.   i = sscanf("25 thompson", "%d%s", &j, buf);
  242.   printf("%d items yielding %d and '%s'\n", i, j, buf);
  243. }
  244.  
  245. /*
  246.  * File Write and Read Test
  247.  *
  248.  * Test that a file can be written to and read from.
  249.  */
  250.  
  251. void frw_test()
  252.  
  253. {
  254.   int i, j, k;
  255.   char buf[80];
  256.  
  257.   puts("\nFile write and read check");
  258. #ifdef atarist
  259.   if ((fp = fopen(TESTFILE, "wt")) != NULL) {
  260. #else
  261.   if ((fp = fopen(TESTFILE, "w")) != NULL) {
  262. #endif
  263.     puts("3 items yielding 56, 789 and '56'");
  264.     puts("1 item yielding 'a72'");
  265.     fprintf(fp, "56789 0123 56a72");
  266. #ifdef atarist
  267.     if (freopen(TESTFILE, "rt", fp) != fp)
  268. #else
  269.     if (freopen(TESTFILE, "r", fp) != fp)
  270. #endif
  271.       puts("Cannot open file for reading");
  272.     else {
  273.       i = fscanf(fp, "%2d%d%*d %[0-9]", &j, &k, buf);
  274.       printf("%d items yielding %d, %d and '%s'\n", i, j, k, buf);
  275.       i = fscanf(fp, "%s", buf);
  276.       printf("%d item yielding '%s'\n", i, buf);
  277.       fclose(fp);
  278.     }
  279.   }
  280. }
  281.  
  282. /*
  283.  * File Seek Test
  284.  *
  285.  * Test that seek operations within files work.
  286.  */
  287.  
  288. void fs_test()
  289.  
  290. {
  291.   int i, j;
  292.  
  293.   puts("\nFile seek test");
  294. #ifdef atarist
  295.   if ((fp = fopen(TESTFILE, "wb")) != NULL) {
  296. #else
  297.   if ((fp = fopen(TESTFILE, "w")) != NULL) {
  298. #endif
  299.     for (i = 0; i < 256; i++)
  300.       putc(i, fp);
  301. #ifdef atarist
  302.     if (freopen(TESTFILE, "rb", fp) != fp)
  303. #else
  304.     if (freopen(TESTFILE, "r", fp) != fp)
  305. #endif
  306.       puts("Cannot open file for reading");
  307.     else {
  308.       for (i = 1; i <= 255; i++) {
  309.         printf("\r%3d ", i);
  310.     fflush(stdout);
  311.         fseek(fp, (long) -i, SEEK_END);
  312.     if ((j = getc(fp)) != 256-i) {
  313.       printf("SEEK_END failed %d\n", j);
  314.       break;
  315.     }
  316.     if (fseek(fp, (long) i, SEEK_SET)) {
  317.       puts("Cannot SEEK_SET");
  318.       break;
  319.     }
  320.     if ((j = getc(fp)) != i) {
  321.       printf("SEEK_SET failed %d\n", j);
  322.       break;
  323.     }
  324.     if (fseek(fp, (long) i, SEEK_SET)) {
  325.       puts("Cannot SEEK_SET");
  326.       break;
  327.     }
  328.     if (fseek(fp, (long) (i >= 128 ? -128 : 128), SEEK_CUR)) {
  329.       puts("Cannot SEEK_CUR");
  330.       break;
  331.     }
  332.     if ((j = getc(fp)) != (i >= 128 ? i-128 : i+128)) {
  333.       printf("SEEK_CUR failed %d\n", j);
  334.       break;
  335.     }
  336.       }
  337.       if (i > 255)
  338.     puts("ok");
  339.       fclose(fp);
  340.     }
  341.   }
  342. }
  343.  
  344. /*
  345.  * Test gets()
  346.  *
  347.  * Checks that gets() works.
  348.  */
  349.  
  350. void gets_test()
  351.  
  352. {
  353.   char buf[80];
  354.  
  355.   puts("\nGets functionality");
  356.   puts("... Type a line and have it echoed ...");
  357.   gets(buf);
  358.   puts(buf);
  359. }
  360.  
  361. /*
  362.  * Fputs Test
  363.  *
  364.  * Check that fputs() works into unbuffered streams.
  365.  */
  366.  
  367. void fputs_test()
  368.  
  369. {
  370.   puts("\nUnbuffered fputs test");
  371. #ifdef atarist
  372.   if ((fp = fopen(TTY, "wt")) != NULL) {
  373. #else
  374.   if ((fp = fopen(TTY, "w")) != NULL) {
  375. #endif
  376.     setbuf(fp, (char *) 0);
  377.     puts("ABCDEFGH<pause>");
  378.     puts("ABCDEFGH<pause>");
  379.     fputs("ABCDEFGH", fp), sleep(1);
  380.     fputs("\nABCDEFGH", fp), sleep(1);
  381.     fclose(fp);
  382.     puts("");
  383.   }
  384. }
  385.  
  386. /*
  387.  * Fprintf Test
  388.  *
  389.  * Check that fprintf() works into unbuffered streams.
  390.  */
  391.  
  392. void fprint_test()
  393.  
  394. {
  395.   puts("\nUnbuffered fprintf test");
  396. #ifdef atarist
  397.   if ((fp = fopen(TTY, "wt")) != NULL) {
  398. #else
  399.   if ((fp = fopen(TTY, "w")) != NULL) {
  400. #endif
  401.     setbuf(fp, (char *) 0);
  402.     puts("ABCDEFGH<pause>");
  403.     puts("ABCDEFGH<pause>");
  404.     fprintf(fp, "ABCDEFGH"), sleep(1);
  405.     fprintf(fp, "\nABCDEFGH"), sleep(1);
  406.     fclose(fp);
  407.     puts("");
  408.   }
  409. }
  410.  
  411. /*
  412.  * Fgets Test
  413.  *
  414.  * Check that fgets() works.
  415.  */
  416.  
  417. void fgets_test()
  418.  
  419. {
  420.   char buf[80];
  421.  
  422.   puts("\nFgets functionality");
  423.   puts("a");
  424.   puts("<pause>ab");
  425.   puts("<pause>abc");
  426.   puts("<pause>abcd");
  427.   puts("<pause>abcde");
  428.   puts("<pause>abcdef");
  429.   puts("<pause>abcdefg<pause>");
  430.   puts("<pause>abcdefg<pause>h");
  431. #ifdef atarist
  432.